package com.qianying.controller.admin;

import com.qianying.comment.CommonResult;
import com.qianying.dto.WorkOrderDTO;
import com.qianying.entity.WorkOrder;
import com.qianying.service.IWorkOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/workOrder")
@Tag(name = "工单管理", description = "工单增删改查接口")
public class WorkOrderController {

    @Autowired
    private IWorkOrderService workOrderService;

    @PostMapping("/add")
    @Operation(summary = "新增工单")
    public CommonResult<?> addOrder(@RequestBody WorkOrderDTO workOrderDTO,
                                    HttpServletRequest serverHttpRequest) {
        if (workOrderDTO == null) {
            return CommonResult.failed("创建失败！");
        }
        Long userId= (Long) serverHttpRequest.getAttribute("userId");
        if (userId==null) {
            return CommonResult.failed("缺少用户身份信息认证！");
        }
        workOrderDTO.setUserId(userId);

        return workOrderService.addOrder(workOrderDTO);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据ID删除工单")
    public boolean deleteOrder(@PathVariable Long id) {
        return workOrderService.removeById(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单")
    public boolean updateOrder(@RequestBody WorkOrder order) {
        return workOrderService.updateById(order);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询工单")
    public WorkOrder getOrder(@PathVariable Long id) {
        return workOrderService.getById(id);
    }

    @GetMapping("/list")
    @Operation(summary = "查询全部工单")
    public List<WorkOrder> listOrders() {
        return workOrderService.list();
    }
}
